package org.gd.hellomysql;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Driver;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

@Deprecated
public class MySQL
{
    private String error = "error not found :)";
    private static final String er = "error not found :)";
    private Connection connection = null;

    private final static String driverPath = Driver.class.getName();
    private final static String url = "jdbc:mysql://localhost:3306/";

    private final static String default_dbName = "hibernate";
    private final static String default_login = "root";
    private final static String default_password = "root";
    /** ****************************************************************************************************
     * @author Gorhover_Dmitriy
     * @category DataBase
     *
     * @param dbName
     * @return result [true/false]
     * @throws java.sql.SQLException
     */
    public boolean createDB(String dbName) throws SQLException
    {
        if((getConnection() != null)&&(dbName.equalsIgnoreCase("") != true)) {
            String command = "CREATE SCHEMA `" + dbName + "` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;";
            Statement statement = this.getConnection().createStatement();
            return !statement.execute(command);
        }
        else
            return false;
    }

    /** ****************************************************************************************************
     * @author Gorhover_Dmitriy
     * @category DataBase
     *
     * @param dbName
     * @return result [true/false]
     * @throws java.sql.SQLException
     */
    public boolean dropDB(String dbName) throws SQLException
    {
        if(getConnection() != null) {
            String command = "DROP SCHEMA IF EXISTS `" + dbName + "`;";
            Statement statement = this.getConnection().createStatement();
            return !statement.execute(command);
        }
        else
            return false;
    }
    /** ****************************************************************************************************
     * @author Gorhover_Dmitriy
     * @category DataBase
     *
     * @param dbName
     * @param tableName
     * @return result [true/false]
     * @throws java.sql.SQLException
     */
    public boolean createTable_temp(String dbName, String tableName) throws SQLException
    {
        if(getConnection() != null) {
            String command = "CREATE TABLE  `" + dbName + "`.`" + tableName + "` (" +
                    "`n1` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , " +
                    "`n2` TEXT NOT NULL , " +
                    "`n3` VARCHAR( 250 ) NOT NULL , " +
                    "`n4` INT NOT NULL , " +
                    "`n5` INT NOT NULL" +
                    ") " +
                    "ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;";

            Statement statement = this.getConnection().createStatement();
            return !statement.execute(command);
        }
        else
            return false;
    }
    /** ****************************************************************************************************
     * @author Gorhover_Dmitriy
     * @category DataBase
     *
     * @return
     * @throws java.sql.SQLException
     */
    public boolean createTable(String command) throws SQLException
    {
        if(getConnection() != null) {
            Statement statement = this.getConnection().createStatement();
            return !statement.execute(command);
        }
        else
            return false;
    }
    /** ****************************************************************************************************
     * @author Gorhover_Dmitriy
     * @category DataBase
     *
     * @param dbName
     * @param tableName
     * @return
     * @throws java.sql.SQLException
     */
    public boolean dropTable(String dbName, String tableName) throws SQLException
    {
        if(getConnection() != null) {
            String command = "DROP TABLE `" + dbName + "`.`" + tableName + "`;";
            Statement statement = this.getConnection().createStatement();
            return !statement.execute(command);
        }
        else
            return false;
    }
    /** ****************************************************************************************************
     * @author Gorhover_Dmitriy
     * @category DataBase
     *
     * @return DataBase names
     * @throws java.sql.SQLException
     */
    public ResultSet showDataBases() throws SQLException
    {
        if(getConnection() != null) {

            String command = "SHOW DATABASES;";
            Statement statement = this.getConnection().createStatement();
            return statement.executeQuery(command);
        }
        else
            return null;
    }
    /** ****************************************************************************************************
     * @author Gorhover_Dmitriy
     * @category DataBase
     *
     * @return DataBase names
     * @throws java.sql.SQLException
     */
    public ResultSet showTables(String dbName) throws SQLException
    {
        String command = null;
        Statement statement = null;
        if(getConnection() != null) {
            command = "USE " + dbName.trim() + ";";
            statement = this.getConnection().createStatement();
            statement.executeQuery(command);

            command = "SHOW TABLES;";
            statement = this.getConnection().createStatement();
            return statement.executeQuery(command);
        }
        else
            return null;
    }
    // ****************************************************************************************************
    public ResultSet selectAllFromTable(String dbName, String tableName) throws SQLException
    {
        if(getConnection() != null) {

            String command = "SELECT * FROM `" + dbName + "`.`" + tableName + "`;";
            Statement statement = this.getConnection().createStatement();
            return statement.executeQuery(command);
        }
        else
            return null;
    }









    // ****************************************************************************************************
    // ****************************************************************************************************
    // ****************************************************************************************************
    /**
     * @author Gorhover_Dmitriy
     * @category DataBase
     */
    public MySQL() {
        this(default_dbName, default_login, default_password);
    }
    /** ******************************
     * @author Gorhover_Dmitriy
     * @category DataBase
     *
     * @param dbName
     */
    public MySQL(String dbName) {
        this(dbName, default_login, default_password);
    }
    /** ******************************
     * @author Gorhover_Dmitriy
     * @category DataBase
     *
     * @param dbName
     * @param login
     * @param password
     */

    public MySQL(String dbName, String login, String password)
    {
        boolean flag = false;
        try {
            // Class.forName(driverPath).newInstance();
            DriverManager.registerDriver((Driver) Class.forName(driverPath).newInstance());
        }
        catch (InstantiationException e1) {
            // e1.printStackTrace();
            // e1.getClass().getName();
            //this.setError(Errors.errorMap_StringKey.get("InstantiationException"));
            System.err.println(this.getErrorString());
            flag = true;
        }
        catch (IllegalAccessException e1) {
            // e1.printStackTrace();
            //this.setError(Errors.IllegalAccessException);
            System.err.println(this.getErrorString());
            flag = true;
        }
        catch (ClassNotFoundException e1) {
            // e1.printStackTrace();
            //this.setError(Errors.ClassNotFoundException);
            System.err.println(this.getErrorString());
            flag = true;
        }
        catch (SQLException e1) {
            // e1.printStackTrace();
            //this.setError(Errors.SQLException);
            System.err.println(this.getErrorString());
            flag = true;
        }

        if(!flag) {
            Connection conn = null;
            try {
                conn = (Connection) DriverManager.getConnection(this.url + dbName, login, password);
                if(conn == null) {
                    //this.setError(Errors.DEFAULT);
                    // System.out.println(this.getError());
                }
            }
            catch (SQLException e) {
                // e.printStackTrace();
                //this.setError(Errors.SQLException);
                // System.out.println(this.getError());
            }
            setConnection(conn);
        }
    }
    // ****************************************************************************************************
    public boolean getError() {
        if(error.equalsIgnoreCase(er))
            return false;
        else
            return true;
    }
    // ******************************
    public String getErrorString() {
        return error;
    }
    // ******************************
    @SuppressWarnings("unused")
    private void setError(String error) {
        this.error = error;
    }
    // ******************************
    public Connection getConnection() {
        return connection;
    }
    // ******************************
    private void setConnection(Connection connection) {
        this.connection = connection;
    }
    // ******************************
    public void endConnection() throws SQLException {
        this.connection.close();
    }
    // ******************************
}